/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package database;

/**
 *
 * @author User
 */
import entity.Employee;
import iface.IManagerDB;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import manager.DisabledEmployeeManager;
import manager.EmployeeManager;

public class EmployeeDB implements IManagerDB<EmployeeManager, DisabledEmployeeManager> {

    private CallableStatement cstmt;
    private java.sql.Connection conn;
    private ResultSet rs;
    
    public EmployeeDB(Connection conn) {
        this.conn = conn.getConn();
    }

    @Override
    public void loadData(EmployeeManager t, boolean value) {
        try {
            cstmt = conn.prepareCall("{call sp_GetEmployee(?)}");
            cstmt.setBoolean("Status", value);
            rs = cstmt.executeQuery();
            while (rs.next()) {
                int employeeID = rs.getInt("EmployeeID");
                String employeeName = rs.getString("EmployeeName");
                String address = rs.getString("Address");
                String phoneNumber = rs.getString("PhoneNumber");
                int departmentID = rs.getInt("DepartmentID");
                boolean status = rs.getBoolean("Status");
                boolean gender = rs.getBoolean("Gender");

                Employee employee = new Employee(employeeID, employeeName, address, phoneNumber, departmentID, status, gender);
                t.add(employee);
            }
        } catch (SQLException ex) {
            Logger.getLogger(EmployeeDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public ArrayList<Employee> getEmployeeOfDepartment(int departmentID) {
        ArrayList<Employee> alEmployee = new ArrayList<Employee>();
        try {
            cstmt = conn.prepareCall("{call sp_GetEmployeeForDepartment(?)}");
            cstmt.setInt("DepartmentID", departmentID);
            rs = cstmt.executeQuery();
            while (rs.next()) {
                int employeeID = rs.getInt("EmployeeID");
                String employeeName = rs.getString("EmployeeName");
                String address = rs.getString("Address");
                String phoneNumber = rs.getString("PhoneNumber");
                boolean status = rs.getBoolean("Status");
                boolean gender = rs.getBoolean("Gender");
                Employee employee = new Employee(employeeID, employeeName, address, phoneNumber, departmentID, status, gender);
                alEmployee.add(employee);
            }
        } catch (SQLException ex) {
            Logger.getLogger(EmployeeDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return alEmployee;
    }

    @Override
    public void insert(Object object) {
        Employee employee = (Employee) object;
        try {
            cstmt = conn.prepareCall("{call sp_InsertEmployee(?,?,?,?,?,?,?,?)}");
            cstmt.setInt(1, employee.getEmployeeID());
            cstmt.setString(2, employee.getEmployeeName());
            cstmt.setString(3, employee.getAddress());
            cstmt.setString(4, employee.getPhoneNumber());
            cstmt.setString(5, employee.getPhoneNumber());
            cstmt.setInt(6, employee.getDepartmentID());
            cstmt.setBoolean(7, employee.isStatus());
            cstmt.setBoolean(8, employee.isGender());

            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(EmployeeDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void update(Object object) {
        Employee employee = (Employee) object;
        try {
            cstmt = conn.prepareCall("{call sp_UpdateEmployee(?,?,?,?,?,?,?,?)}");
            cstmt.setInt(1, employee.getEmployeeID());
            cstmt.setString(2, employee.getEmployeeName());
            cstmt.setString(3, employee.getAddress());
            cstmt.setString(4, employee.getPhoneNumber());
            cstmt.setString(5, employee.getPhoneNumber());
            cstmt.setInt(6, employee.getDepartmentID());
            cstmt.setBoolean(7, employee.isStatus());
            cstmt.setBoolean(8, employee.isGender());

            cstmt.executeUpdate();
            cstmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(EmployeeDB.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public void delete(Object object) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}